// Copyright 2019 orivil.com. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found at https://mit-license.org.

package models

import (
	"encoding/json"
	"strings"
)

type Subject struct {
	ID       string  `json:"id" gorm:"index"`
	Desc     string  `json:"desc"`
	Img      string  `json:"img"`
	Original string  `json:"original" gorm:"index"`
	Type     string  `json:"type" gorm:"index"`
	Title    string  `json:"title" gorm:"index"`
	View     float32 `json:"view" gorm:"index"`
	IsGame   string  `json:"is_game" gorm:"index"`
}

type GameData struct {
	DataID   string     `json:"id"`
	Title    string     `json:"title"`
	Logo     string     `json:"logo"`
	LogoUrl  string     `json:"logo_url"`
	Desc     string     `json:"desc"`
	Question []Question `json:"question"`
	Original string     `json:"original"`
	// 1：单一问题，按选项 key 获得结果
	// 2：按 weight 获得结果
	// 3：按 next 获得结果
	// 8：按 next 获得结果
	Type string `json:"type" gorm:"index"`
	// 主题类型
	Cid               string `json:"cid" gorm:"index"`
	Img               string `json:"img"`
	TplID             string `json:"tpl_id"`
	IsGame            string `json:"is_game"`
	ShareUrl          string `json:"share_url"`
	View              string `json:"view"`
	LikeNum           string `json:"like_num"`
	Utime             string `json:"utime"`
	Subscribe         string `json:"subscribe"`
	NewPlan           string `json:"new_plan"`
	Status            string `json:"status"`
	NeedLogin         string `json:"need_login"`
	PID               string `json:"pid"`
	SpecialResult     string `json:"special_result"`
	SpecialResultData string `json:"special_result_data"`
	NameConfig        string `json:"name_config"`
	HeadImgConfig     string `json:"head_img_config"`
}

func UnmarshalGameData(gd *GameData, data []byte) error {
	schema := make(map[string]interface{}, 3)
	err := json.Unmarshal(data, &schema)
	if err != nil {
		return err
	}
	res := make(map[string]interface{}, len(schema))
	for key, value := range schema {
		if str, ok := value.(string); ok {
			str = strings.TrimSpace(str)
			if strings.HasPrefix(str, "{") && strings.HasSuffix(str, "}") {
				s := make(map[string]interface{}, 3)
				err = json.Unmarshal([]byte(str), &s)
				if err != nil {
					return err
				}
				res[key] = s
			} else if strings.HasPrefix(str, "[") && strings.HasSuffix(str, "]") {
				var s []map[string]interface{}
				err = json.Unmarshal([]byte(str), &s)
				if err != nil {
					return err
				}
				res[key] = s
			} else {
				res[key] = str
			}
		} else {
			res[key] = str
		}
	}
	data, err = json.Marshal(res)
	if err != nil {
		return err
	} else {
		return json.Unmarshal(data, gd)
	}
}

type Question struct {
	Img      string            `json:"img"`
	Question string            `json:"question"`
	Audio    string            `json:"audio"`
	Answer   map[string]Answer `json:"answer"`
	Weight   string            `json:"weight"`
	Merge    bool              `json:"merge"`
}

type Answer struct {
	Title  string      `json:"title"`
	Weight interface{} `json:"weight"`
	Next   string      `json:"next"`
	Img    string      `json:"img"`
}
